package com.easyadmin.pro.modules.sys.controller;

import com.easyadmin.pro.common.annotation.log.Log;
import com.easyadmin.pro.common.constants.HttpConstants;
import com.easyadmin.pro.common.enums.HttpCodeEnum;
import com.easyadmin.pro.common.exception.BusinessException;
import com.easyadmin.pro.common.page.Pageable;
import com.easyadmin.pro.common.vo.RestResult;
import com.easyadmin.pro.modules.sys.query.DictQuery;
import com.easyadmin.pro.modules.sys.rest.DictInfoResultVo;
import com.easyadmin.pro.modules.sys.vo.CheckDictInfoVo;
import com.easyadmin.pro.modules.sys.vo.DictVo;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import com.easyadmin.pro.modules.sys.entity.DictEntity;
import com.easyadmin.pro.modules.sys.service.IDictService;
import org.springframework.web.bind.annotation.RestController;
import com.easyadmin.pro.common.controller.BaseController;
import java.io.Serializable;
import java.util.List;
import java.util.Objects;

/**
 *  控制层。
 *
 * @author xulin
 * @since 2024-08-19
 */
@RestController
@RequestMapping(HttpConstants.SYS_MODULE_PATH + "/dict/")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class DictController extends BaseController {

    private final IDictService iDictService;

    /**
     * 添加。
     *
     * @param dictVo
     * @return
     */
    @Log(methodName = "添加字典")
    @PostMapping("save")
    public RestResult save(@RequestBody @Validated DictVo dictVo) {
        iDictService.generatorDict(dictVo);
        return success();
    }

    /**
     * 根据主键删除。
     *
     * @param id 主键
     * @return
     */
    @DeleteMapping("remove/{id}")
    public RestResult remove(@PathVariable Serializable id) {
        iDictService.removeDict(id);
        return success();
    }

    /**
     * 根据主键获取详细信息。
     *
     * @param id 主键
     * @return 详情
     */
    @Log(methodName = "获取字典详细信息")
    @GetMapping("getInfo/{id}")
    public RestResult getInfo(@PathVariable Serializable id) {
        DictInfoResultVo dictInfoResultVo = new DictInfoResultVo();
        BeanUtils.copyProperties(iDictService.getById(id), dictInfoResultVo);
        return success(dictInfoResultVo);
    }

    /**
     * 分页查询。
     *
     * @param dictQuery
     * @return 分页对象
     */
    @Log(methodName = "数据字典查询")
    @GetMapping("page")
    public RestResult page(DictQuery dictQuery) {
        return success(Pageable.of(dictQuery, () -> iDictService.queryPage(dictQuery)));
    }

    /**
     * 校验字典编码
     * @param checkDictInfoVo
     * @return
     */
    @PostMapping("checkDictCode")
    public RestResult checkDictCode(@RequestBody @Validated CheckDictInfoVo checkDictInfoVo) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(DictEntity::getDictCode, checkDictInfoVo.getDictCode());
        if (Objects.nonNull(checkDictInfoVo.getDictId())) {
            queryWrapper.ne(DictEntity::getId, checkDictInfoVo.getDictId());
        }
        long count = iDictService.count(queryWrapper);
        if (count > 0) {
            throw new BusinessException(HttpCodeEnum.DICT_CODE_EXISTS_ERROR);
        }
        return success();

    }

    /**
     * 检查字典数据
     * @param id
     * @return
     */
    @GetMapping("checkDictInformation/{id}")
    public RestResult checkDictInformation(@PathVariable Serializable id) {
        iDictService.checkDictInformation(id);
        return success();
    }

    /**
     * 删除字典数据
     * @param id
     * @return
     */
    @DeleteMapping("removeData/{id}")
    public RestResult removeData(@PathVariable Serializable id) {
        iDictService.removeData(id);
        return success();
    }

}
